package orderEvaluate

import (
	"time"
	"web/models"
	"web/pkg/util"
)

// ROrderEvaluate 订单评价详细响应结构
type ROrderEvaluate struct {
	Id          uint   `json:"id"`
	OrderId     uint64 `json:"order_id"`
	Uid         int    `json:"uid"`
	Star        uint8  `json:"star"`
	StarText    string `json:"star_text"` // 星级文本描述
	Content     string `json:"content"`
	Tag         string `json:"tag"`
	IsAnony     uint8  `json:"is_anony"`
	IsAnonyText string `json:"is_anony_text"` // 匿名状态文本
	CreatedAt   string `json:"created_at"`
	DeletedAt   string `json:"deleted_at,omitempty"`
}

// RLintOrderEvaluate 订单评价简要响应结构
type RLintOrderEvaluate struct {
	Id       uint   `json:"id"`
	OrderId  uint64 `json:"order_id"`
	Star     uint8  `json:"star"`
	StarText string `json:"star_text"`
	Content  string `json:"content"`
	IsAnony  uint8  `json:"is_anony"`
}

type EncodeOrderEvaluateService struct {
}

// NewEncodeOrderEvaluateService 创建编码服务实例
func NewEncodeOrderEvaluateService() *EncodeOrderEvaluateService {
	return &EncodeOrderEvaluateService{}
}

// Encode 编码单个订单评价
func (s *EncodeOrderEvaluateService) Encode(evaluate *models.LaOrderEvaluate) *ROrderEvaluate {
	if evaluate == nil {
		return &ROrderEvaluate{}
	}

	return &ROrderEvaluate{
		Id:          uint(evaluate.ID),
		OrderId:     evaluate.OrderID,
		Uid:         evaluate.Uid,
		Star:        evaluate.Star,
		StarText:    s.getStarText(evaluate.Star),
		Content:     evaluate.Content,
		Tag:         evaluate.Tag,
		IsAnony:     evaluate.IsAnony,
		IsAnonyText: s.getAnonyText(evaluate.IsAnony),
		CreatedAt:   s.formatTimestamp(evaluate.CreatedAt),
		DeletedAt:   s.formatTimestamp(evaluate.DeletedAt),
	}
}

// EncodeLint 编码简要订单评价信息
func (s *EncodeOrderEvaluateService) EncodeLint(evaluate *models.LaOrderEvaluate) *RLintOrderEvaluate {
	if evaluate == nil {
		return &RLintOrderEvaluate{}
	}

	return &RLintOrderEvaluate{
		Id:       uint(evaluate.ID),
		OrderId:  evaluate.OrderID,
		Star:     evaluate.Star,
		StarText: s.getStarText(evaluate.Star),
		Content:  evaluate.Content,
		IsAnony:  evaluate.IsAnony,
	}
}

// EncodeList 编码订单评价列表
func (s *EncodeOrderEvaluateService) EncodeList(evaluates []*models.LaOrderEvaluate) []*ROrderEvaluate {
	if len(evaluates) == 0 {
		return make([]*ROrderEvaluate, 0)
	}

	result := make([]*ROrderEvaluate, len(evaluates))
	for i, evaluate := range evaluates {
		result[i] = s.Encode(evaluate)
	}
	return result
}

// EncodeLintList 编码简要订单评价列表
func (s *EncodeOrderEvaluateService) EncodeLintList(evaluates []*models.LaOrderEvaluate) []*RLintOrderEvaluate {
	if len(evaluates) == 0 {
		return make([]*RLintOrderEvaluate, 0)
	}

	result := make([]*RLintOrderEvaluate, len(evaluates))
	for i, evaluate := range evaluates {
		result[i] = s.EncodeLint(evaluate)
	}
	return result
}

// MakePagedResponse 创建分页响应
func (s *EncodeOrderEvaluateService) MakePagedResponse(evaluates []*models.LaOrderEvaluate, nextPageInfo util.NextPageInfo) map[string]interface{} {
	encodedEvaluates := s.EncodeList(evaluates)
	return util.MakePagedResponse(encodedEvaluates, nextPageInfo)
}

// MakePagedLintResponse 创建分页简要响应
func (s *EncodeOrderEvaluateService) MakePagedLintResponse(evaluates []*models.LaOrderEvaluate, nextPageInfo util.NextPageInfo) map[string]interface{} {
	encodedEvaluates := s.EncodeLintList(evaluates)
	return util.MakePagedResponse(encodedEvaluates, nextPageInfo)
}

// getStarText 获取星级文本描述
func (s *EncodeOrderEvaluateService) getStarText(star uint8) string {
	starTexts := map[uint8]string{
		1: "非常不满意",
		2: "不满意",
		3: "一般",
		4: "满意",
		5: "非常满意",
	}

	if text, exists := starTexts[star]; exists {
		return text
	}
	return "未评级"
}

// getAnonyText 获取匿名状态文本
func (s *EncodeOrderEvaluateService) getAnonyText(isAnony uint8) string {
	if isAnony == 1 {
		return "取消匿名"
	}
	return "匿名"
}

// formatTimestamp 格式化时间戳
func (s *EncodeOrderEvaluateService) formatTimestamp(timestamp time.Time) string {
	if timestamp.IsZero() {
		return ""
	}
	return timestamp.Format("2006-01-02 15:04:05")
}
